# QCA replication file 
# "Explaining the politicization of EU trade agreement negotiations over the past 30 years"
# Author: Luca Cabras

# Packages
library(QCA)
library(QCAtools)
library(SetMethods)

# OUTCOME
# Politicization

# CONDITIONS
# Depth
# Economic size
# Institutional set-up
# Public attitude
# Import competition 

## CALIBRATION ##

# Outcome #
QCA_Raw_Data$POL <- calibrate(QCA_Raw_Data$Politicization, 
                        type = "fuzzy", 
                        thresholds = c(0.7, 12.5, 27.3), 
                        logistic = TRUE)

# Conditions #

# Depth:
QCA_Raw_Data$DEP <- calibrate(QCA_Raw_Data$Depth, 
                            type = "crisp", 
                            thresholds = c(8.1))

# Economic size:
QCA_Raw_Data$SIZ <- calibrate(QCA_Raw_Data$Size, 
                            type = "crisp", 
                            thresholds = c(0.095))

# Public attitude:
QCA_Raw_Data$ATD <- calibrate(QCA_Raw_Data$Attitude, 
                              type = "crisp", 
                              thresholds = c(56.5))

## Import competition:
QCA_Raw_Data$IMP <- calibrate(QCA_Raw_Data$Competition, 
                              type = "crisp", 
                              thresholds = c(50))

# Institutional set-up:
QCA_Raw_Data$INST <- calibrate(QCA_Raw_Data$Institutions, 
                            type = "crisp", 
                            thresholds = c(0.5))

## Analysis of Necessity ##
QCAfit( x = QCA_Raw_Data[,c("DEP", "SIZ", "INST" , "ATD", "IMP")],
        y = QCA_Raw_Data$POL)

## Analysis of Sufficiency ##

# Object containing the conditions
conds <- c("DEP", "SIZ", "INST", "ATD", "IMP")

# TRUTH TABLE #
TT_y <- truthTable(QCA_Raw_Data,
                   outcome = "POL",
                   conditions = conds,
                   incl.cut = 0.8,
                   complete = TRUE,
                   pri.cut = 0.5,
                   sort.by = c("incl", "n"),
                   show.cases = TRUE)
TT_y

# MINIMIZATION #

# CONSERVATIVE SOLUTION:
sol_yc <- minimize(TT_y, 
                   details = TRUE,
                   use.tilde = TRUE)
sol_yc

# PARSIMONIOUS SOLUTION:
sol_yp <- minimize(TT_y, 
                   details = TRUE, 
                   include = "?",
                   use.tilde = TRUE,
                   row.dom = TRUE)
sol_yp

# INTERMEDIATE SOLUTION
sol_yi <- minimize(TT_y, 
                   details = TRUE, 
                   include = "?",
                   dir.exp = c(1, 1, 1, 0, 1),
                   use.tilde = TRUE,
                   row.dom = TRUE)
sol_yi

# The intermediate solution "From C1P2" is discussed in the paper
